package zw_701_800.zw_747_至少是其他数字两倍的最大数;

import java.util.Arrays;

class Solution {
    public static void main(String[] args) {
        int[] nums = {3, 6, 1, 0};
        int index = dominantIndex(nums);
        System.out.println(index);
    }


    public static int dominantIndex(int[] nums) {
        int n = nums.length;
        if (n == 1) return 0;
        int a = -1, b = 0;
        for (int i = 1; i < n; i++) {
            if (nums[i] > nums[b]) {
                a = b;
                b = i;
            } else if (a == -1 || nums[i] > nums[a]) {
                a = i;
            }
        }
        return nums[b] >= nums[a] * 2 ? b : -1;
    }

//    public static int dominantIndex(int[] nums) {
//        if (nums.length == 1) return 0;
//        int[] clone = nums.clone();
//        Arrays.sort(clone);
//        if (clone[nums.length - 1] >= 2 * clone[nums.length - 2]) {
//            for (int i = 0; i < nums.length; i++) {
//                if (nums[i] == clone[nums.length - 1]) return i;
//            }
//        }
//        return -1;
//    }
}
